<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT      : rubik-base
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 *
 * </pre>
 * 
 * @version 		1.0
 * @author			Dang Nhon Hoa <hoadn@rubik.com.vn>
 * @access			public
 * @package			rubik.base.html
 *
 * Created on	: May 25, 2008
 */
RCoreImporter::cls( "html.BasicHtmlUploadControl" );

class AjaxHtmlUploadControl extends BasicHtmlUploadControl {
	
	var $_uploadLabel = "Upload image";
	var $_uploadUrlProcessing = "ext/ajaxfileupload/doajaxfileupload.php";
	var $_basicInputName = null;
	
	/**
	 * Construtor
	 *
	 * @param string $inputName
	 * @param string $pathFromRoot Ex: "uploadFiles/images/"
	 * @param string/array $valueOrArrayContain if array normaly it should be: $_GET, $_POST, $_REQUEST
	 * @param string $supportedTypes Ex: ".mp3, .swf, .wma" 
	 * @param boolean $supportURL
	 * @return BasicHtmlUploadControl
	 */
	function __construct($inputName, $pathFromRoot=null, $valueOrArrayContain = null, $supportedTypes = null, $supportURL = false) {
		$this->_basicInputName = $inputName;
		$this->_inputName = $inputName."_file";
		$this->_supportedTypes = $supportedTypes;
		$this->_pathFromRoot = $pathFromRoot;
		$this->setValue ( $valueOrArrayContain );
		$this->_supportURL = $supportURL;
	}
	
	/**
	 * Set value for this
	 *
	 * @param string/array $valueOrArrayContain
	 */
	function setValue($valueOrArrayContain) {
		if (is_object($valueOrArrayContain)) {
			$this->_value = RubikHelper::getValueFrom($valueOrArrayContain, $this->_basicInputName);
			return;
		}
		
		return parent::setValue($valueOrArrayContain);
	}
	
	/**
	 * Set the label upload text for this control
	 * @param $text
	 */
	function setUploadLabel($text) {
		$this->_uploadLabel = $text;
	}
	
	/**
	 * Set the URL for processing the ajax upload
	 * @param $url
	 */
	function setUploadUrlProcessing($url) {
		$this->_uploadUrlProcessing = $url;
	}
	
	/**
	 * Create the HTML text for doing upload
	 *
	 * @param int $inputSize view size of html
	 * @param string $cssCls
	 * @return string: html string
	 */
	function createUploadFormHtml($attribute = '', $showAcceptMsg = true) {
		static $id = 0;
		$id++;
		$idName = $this->_inputName.$id;
		
		SysResponse::addJavascriptFile('ext/ajaxfileupload/ajaxfileupload.js');
		$html = parent::createUploadFormHtml($attribute, $showAcceptMsg, $id);
		$html .= '<input type="hidden" name="'.$this->_basicInputName.'" value="'.$this->_value.'" id="'.$this->_inputName.'_id" />';
		$path = base64_encode($this->_pathFromRoot);
		$html .= '<input type="hidden" name="path_'.$this->_inputName.'" value="'.$path.'" id="path_'.$this->_inputName.'_id" />';
		
		
		$js = '
			function showAjaxUpload(id) {
				$("#text_"+id).hide(500);
				$("#upload_"+id).show(500);
			}
			
	function ajaxFileUpload(fileEle, uploadUrl)
	{
		if (!fileEle.value) {
			return;
		}

		var id = fileEle.id;
		$("#uploading_" + id).show();
		$("#upload_" + id).hide();

		$.ajaxFileUpload
		(
			{
				url: uploadUrl,
				secureuri:false,
				fileElementId:"upload_" + id,
				expectedFolder:"'.$path.'",
				dataType: "json",
				success: function (data, status)
				{
					if(typeof(data.error) != "undefined")
					{
						if(data.error != "")
						{
							// set value for hidden field
							alert(data.error);
						}else
						{
							var id = "#" + fileEle.name + "_id";
							$(id).val(data.file);
							if (typeof processSuccess == "function") {
								processSuccess(data, id);
							}
							if (typeof processSuccessVideo == "function") {
								processSuccessVideo(data, id);
							}
						}
					}
					
				},
				error: function (data, status, e)
				{
					alert(e);
				},
				complete: function(xml, status) {
					$("#uploading_" + id).hide();
					$("#upload_" + id).show();
					
					// clear current data in file input
					$("#" + id).change(function(){ajaxFileUpload(document.getElementById(id), uploadUrl)});
					emptyValue(document.getElementById(id));
				}
			}
		)
		
		return false;

	}
		';
		
		SysResponse::addJavascriptContent($js);
		SysResponse::addJavascriptContent('
			$(document).ready(function(){
				$("#'.$idName.'").change(function(){ajaxFileUpload(this, "'.$this->_uploadUrlProcessing.'")});
			});
		');
		
		$prevHtml = $this->getPreviewHtml();
		
		$imgUploadingLink = SYS_ROOT_URL."ext/ajaxfileupload/ajax-loader.gif";
		$fullHtml = '<div class="ajax-preview">'.$prevHtml.'
			<div class="ajax-upload-field">
				<div id="text_'.$idName.'" onclick="showAjaxUpload(\''.$idName.'\')">
					<a href="#" onclick="return false;">'.$this->_uploadLabel.'</a>
				</div>
				<div id="upload_'.$idName.'" style="display:none;">
					'.$html.'
				</div>
				<div id="uploading_'.$idName.'" style="display:none;">
					<img src="'.$imgUploadingLink.'" />
				</div>
			</div>
			</div>
		';
		
		return $fullHtml;
	}
	
	function getPreviewHtml() {
		return '';
	}
	
	/**
	 * Static function
	 * @param $valueOrArrayContain
	 * @return unknown_type
	 */
	function getAjaxFileResult($valueOrArrayContain) {
		$pathName = 'path_'.$this->_inputName;
		$path = $valueOrArrayContain[$pathName];
		if ($path) {
			$path = base64_decode($path);
			$this->_pathFromRoot = $path;
		}
		
		$this->setValue($valueOrArrayContain);
		if ($this->hasError()) {
			$result['error'] = $this->getError();
		}
		else {
			$result['error'] = '';
			$result['file'] = $this->getValueWithoutUrl();
			$result['msg'] = 'Upload successfull';
		}
		
		return json_encode($result);
	}
}
?>
